﻿using System;
using System.Web.Services;
using System.Collections;
using Ultimus.WFServer;
using Ultimus.OC;
using System.Data.OleDb;
using System.IO;
using System.Collections.Specialized;
using System.Data;

namespace H2.NET.Web.Workflow
{
    /// <summary>
    /// WebService 的摘要说明
    /// </summary>
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [WebService(Description = "当前 Web Service 允许您发起、取消流程，提交、退回步骤并允许获取流程及步骤属性信息。", Namespace = "http://www.ultimus.com")]
    public class Webservice : System.Web.Services.WebService
    {
        #region 构造函数
        /// <summary>
        /// 
        /// </summary>
        public Webservice()
        {

        }
        #endregion

        #region 取消实例
        /// <summary>
        /// 
        /// </summary>
        /// <param name="strProcessName"></param>
        /// <param name="strUserName"></param>
        /// <param name="nIncidentNumber"></param>
        /// <param name="strSummary"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        [WebMethod(Description = "当前方法用于取消实例。")]
        public bool AbortIncident(string strProcessName, string strUserName, int nIncidentNumber, string strSummary, out string strError)
        {
            Incident incident = new Incident();
            try
            {

                incident.LoadIncident(strProcessName, nIncidentNumber);
                return incident.AbortIncident(strUserName, strSummary, out strError);
            }
            catch (Exception exception)
            {
                strError = exception.Message;
                return false;
            }
        }
        #endregion

        #region 提交步骤任务
        /// <summary>
        /// 
        /// </summary>
        /// <param name="strProcessName"></param>
        /// <param name="strUserName"></param>
        /// <param name="nIncidentNumber"></param>
        /// <param name="strStepName"></param>
        /// <param name="strSummary"></param>
        /// <param name="strMemo"></param>
        /// <param name="varList"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        [WebMethod(Description = "当前方法用于提交步骤任务")]
        public bool CompleteStep(string strProcessName,string strUserName, int nIncidentNumber, string strStepName, string strSummary, string strMemo, Variable[] varList, out string strError)
        {
            Tasklist tasklist = new Tasklist();
            TasklistFilter filter = new TasklistFilter();
            Task firstTask = new Task();
            try
            {
                filter.strProcessNameFilter = strProcessName;
                filter.strStepLabelFilter = strStepName;
                filter.nIncidentNo = nIncidentNumber;
                filter.strArrUserName = new string[] { strUserName };
                tasklist.LoadFilteredTasks(filter);
                firstTask = tasklist.GetFirstTask();
                if (firstTask == null)
                {
                    strError = "参数不正确。";
                    return false;
                }
                int nParamIncidentNo = nIncidentNumber;
                return firstTask.Send(varList, strMemo, strSummary, ref nParamIncidentNo, out strError);
            }
            catch (Exception exception)
            {
                strError = exception.Message;
                return false;
            }
        }
        #endregion

        #region 获取用户活动任务
        /// <summary>
        /// 
        /// </summary>
        /// <param name="strProcessName"></param>
        /// <param name="strUserName"></param>
        /// <param name="taskList"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        [WebMethod(Description = "当前方法用于获取用户活动任务。")]
        public bool GetActiveTasks(string strProcessName, string strUserName, out TaskInfo[] taskList, out string strError)
        {
            Tasklist tasklist = new Tasklist();
            TasklistFilter filter = new TasklistFilter();
            Task taskInfo = new Task();
            try
            {
                filter.strProcessNameFilter = strProcessName;
                filter.strArrUserName = new string[] { strUserName };
                filter.nFiltersMask = (Filters.nFilter_Urgent | Filters.nFilter_Overdue) | Filters.nFilter_Current;
                tasklist.LoadFilteredTasks(filter);
                taskList = new TaskInfo[tasklist.GetTasksCount()];
                taskInfo = tasklist.GetFirstTask();
                if (taskInfo == null)
                {
                    taskList = null;
                    strError = "参数不正确。";
                    return false;
                }
                for (int i = 0; i < tasklist.GetTasksCount(); i++)
                {
                    taskList[i] = new TaskInfo(taskInfo);
                    taskInfo = tasklist.GetNextTask();
                    if (taskInfo == null)
                    {
                        break;
                    }
                }
                strError = null;
                return true;
            }
            catch (Exception exception)
            {
                taskList = null;
                strError = exception.Message;
                return false;
            }
        }
        #endregion

        #region 获取发起步骤信息
        /// <summary>
        /// 
        /// </summary>
        /// <param name="strProcessName"></param>
        /// <param name="strUserName"></param>
        /// <param name="varList"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        [WebMethod(Description = "当前方法用于获取发起步骤信息。")]
        public bool GetLaunchInformation(string strProcessName,string strUserName, out Variable[] varList, out string strError)
        {
            Tasklist tasklist = new Tasklist();
            TasklistFilter filter = new TasklistFilter();
            Task firstTask = new Task();
            try
            {
                filter.strProcessNameFilter = strProcessName;
                filter.nIncidentNo = 0;
                filter.strArrUserName = new string[] { strUserName };
                tasklist.LoadFilteredTasks(filter);
                firstTask = tasklist.GetFirstTask();
                if (firstTask == null)
                {
                    strError = "参数不正确。";
                    varList = null;
                    return false;
                }
                if (firstTask.GetAllTaskVariables(out varList, out strError))
                {
                    return true;
                }
                varList = null;
                return false;
            }
            catch (Exception exception)
            {
                varList = null;
                strError = exception.Message;
                return false;
            }
        }
        #endregion

        #region 获取任务信息
        /// <summary>
        /// 
        /// </summary>
        /// <param name="strProcessName"></param>
        /// <param name="strUserName"></param>
        /// <param name="nIncidentNumber"></param>
        /// <param name="strStepName"></param>
        /// <param name="varList"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        [WebMethod(Description = "当前方法用于获取任务信息。")]
        public bool GetTaskInformation(string strProcessName,string strUserName, int nIncidentNumber, string strStepName, out Variable[] varList, out string strError)
        {
            Tasklist tasklist = new Tasklist();
            TasklistFilter filter = new TasklistFilter();
            Task firstTask = new Task();
            try
            {
                filter.strProcessNameFilter = strProcessName;
                filter.strStepLabelFilter = strStepName;
                filter.nIncidentNo = nIncidentNumber;
                filter.strArrUserName = new string[] { strUserName };
                tasklist.LoadFilteredTasks(filter);
                firstTask = tasklist.GetFirstTask();
                if (firstTask == null)
                {
                    strError = "参数不正确。";
                    varList = null;
                    return false;
                }
                if (firstTask.GetAllTaskVariables(out varList, out strError))
                {
                    return true;
                }
                varList = null;
                return false;
            }
            catch (Exception exception)
            {
                varList = null;
                strError = exception.Message;
                return false;
            }
        }
        #endregion

        #region 发起流程实例
        /// <summary>
        /// 
        /// </summary>
        /// <param name="strProcessName"></param>
        /// <param name="strUserName"></param>
        /// <param name="strSummary"></param>
        /// <param name="varList"></param>
        /// <param name="nIncidentNo"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        [WebMethod(Description = "当前方法用于发起流程实例。")]
        public bool LaunchIncident(string strProcessName,string strUserName, string strSummary, Variable[] varList, ref int nIncidentNo, out string strError)
        {
            Tasklist tasklist = new Tasklist();
            TasklistFilter filter = new TasklistFilter();
            Task firstTask = new Task();
            try
            {
                filter.strProcessNameFilter = strProcessName;
                filter.nIncidentNo = 0;
                filter.strArrUserName = new string[] { strUserName };
                tasklist.LoadFilteredTasks(filter);
                firstTask = tasklist.GetFirstTask();
                if (firstTask == null)
                {
                    nIncidentNo = -1;
                    strError = "参数不正确。";
                    return false;
                }
                return firstTask.SendFrom(strUserName, varList, null, strSummary, ref nIncidentNo, out strError);
            }
            catch (Exception exception)
            {
                nIncidentNo = -1;
                strError = exception.Message;
                return false;
            }
        }
        #endregion

        #region 退回步骤
        /// <summary>
        /// 
        /// </summary>
        /// <param name="strProcessName"></param>
        /// <param name="strUserName"></param>
        /// <param name="nIncidentNumber"></param>
        /// <param name="strStepName"></param>
        /// <param name="strSummary"></param>
        /// <param name="strMemo"></param>
        /// <param name="varList"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        [WebMethod(Description = "当前方法用于退回步骤。")]
        public bool ReturnStep(string strProcessName,string strUserName, int nIncidentNumber, string strStepName, string strSummary, string strMemo, Variable[] varList, out string strError)
        {
            Tasklist tasklist = new Tasklist();
            TasklistFilter filter = new TasklistFilter();
            Task firstTask = new Task();
            try
            {
                filter.strProcessNameFilter = strProcessName;
                filter.strStepLabelFilter = strStepName;
                filter.nIncidentNo = nIncidentNumber;
                filter.strArrUserName = new string[] { strUserName };
                tasklist.LoadFilteredTasks(filter);
                firstTask = tasklist.GetFirstTask();
                if (firstTask == null)
                {
                    strError = "参数不正确。";
                    return false;
                }
                return firstTask.Return(varList, strMemo, strSummary, out strError);
            }
            catch (Exception exception)
            {
                strError = exception.Message;
                return false;
            }
        }
        #endregion

        #region 系统登陆验证
        /// <summary>
        /// 
        /// </summary>
        /// <param name="strUserName"></param>
        /// <param name="strPassword"></param>
        /// <param name="strError"></param>
        /// <returns></returns>
        [WebMethod(Description = "当前方法用于系统登陆验证。")]
        public bool VerifyUser(string strUserName, string strPassword, out string strError)
        {
            try
            {
                //验证是否合法用户
                strError = "";
                OrgChart oc = new OrgChart();

                //如果验证通过的情况
                return oc.VerifyUser(strUserName, Decode(strPassword));
            }
            catch (Exception exception)
            {
                strError = exception.Message;
                return false;
            }
        }
        #endregion

        #region 解密
        /// <summary>
        /// 
        /// </summary>
        /// <param name="strCode"></param>
        /// <returns></returns>
        private string Decode(string strCode)
        {
            try
            {
                string strDecode = "";

                for (int i = 0; i < strCode.Length; i = i + 4)
                {
                    strDecode += System.Convert.ToChar(Convert.ToInt64(strCode.Substring(i, 4), 16));
                }

                return strDecode;
            }
            catch
            {
                return "";
            }
        }
        #endregion
    }

    public class TaskInfo
    {
        #region 全局变量
        public int m_nIncidentNum;
        public string m_strStepLabel;
        public string m_strSummary;
        #endregion

        #region 构造函数
        /// <summary>
        /// 
        /// </summary>
        public TaskInfo()
        {

        }

        public TaskInfo(Task taskInfo)
        {
            this.m_strStepLabel = taskInfo.strStepName;
            this.m_strSummary = taskInfo.strSummary;
            this.m_nIncidentNum = taskInfo.nIncidentNo;
        }
        #endregion
    }
}




